package com.dragon.manage.action;

import com.alibaba.fastjson.JSONObject;
import com.dragon.manage.bean.ManageUser;
import com.dragon.manage.service.IManageRightService;
import com.dragon.manage.service.IManageService;
import com.dragon.manage.util.LogUtil;
import com.dragon.manage.util.ManageUtil;
import com.dragon.manage.util.RightUtil;
import com.dragon.manage.util.StaticData;
import com.dragon.manage.util.StringUtil;
import com.opensymphony.xwork2.ActionContext;

/**
 * 权限管理
 * @author 34225_000
 *
 */
@SuppressWarnings("rawtypes")
public class ManageRightAction extends BaseAction  {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	private IManageService manageService;
	
	private IManageRightService manageRightService;
	
	
	public void setManageRightService(IManageRightService manageRightService) {
		this.manageRightService = manageRightService;
	}

	public void setManageService(IManageService manageService) {
		this.manageService = manageService;
	}
	
	/**
	 * 获取管理员列表
	 * @return
	 */
	public String getManageUserList(){
		ActionContext ct=ActionContext.getContext();
		JSONObject result = new JSONObject();
		try {
			/*
			 * 再通过sessionid获取用户信息
			 */
			ManageUser manageUser =  ManageUtil.getManageUserBySession(ct, result, 
					request,manageService, RightUtil.getRightId(this.getClass().getName()));
			if(manageUser==null){
				
				ct.put(StaticData.reKey.result.getKey(), result);
				return StaticData.RE_SUCCESS;
			}
			//是否查询后台用户id
			String manageUserIds=request.getParameter("manageUserId");
			//是否分页
			String pages=request.getParameter("page");
			result = manageRightService.getManageUserList(StringUtil.getParam(manageUserIds),
					StringUtil.getParam(pages));
			ct.put(StaticData.reKey.result.getKey(), result);
			return StaticData.RE_SUCCESS;
		} catch (Exception e) {
			LogUtil.error("获取添加比赛的页面报错",e);
			result.put(StaticData.reKey.code.getKey(), StaticData.codeType.other_error.getType());
			ct.put(StaticData.reKey.result.getKey(), result);
			return StaticData.RE_SUCCESS;
		}
	}

	/**
	 * 更新用户信息
	 * @return
	 */
	public String updateManageUser(){
		ActionContext ct=ActionContext.getContext();
		JSONObject result = new JSONObject();
		try {
			/*
			 * 再通过sessionid获取用户信息
			 */
			ManageUser manageUser =  ManageUtil.getManageUserBySession(ct, result, 
					request,manageService, RightUtil.getRightId(this.getClass().getName()));
			if(manageUser==null){
				
				ct.put(StaticData.reKey.result.getKey(), result);
				return StaticData.RE_SUCCESS;
			}
			//是否查询后台用户id
			String manageUserIds=request.getParameter("manageUserId");
			if(manageUserIds==null){
				result.put(StaticData.reKey.code.getKey(), StaticData.codeType.param_error.getType());
				ct.put(StaticData.reKey.result.getKey(), result);
				return StaticData.RE_SUCCESS;
			}
			//是否重置密码
			String isReset=request.getParameter("isReSet");
			//用户权限
			String userRight = request.getParameter("userRight");
			//用户状态
			String isDelete =  request.getParameter("isDelete");
			result = manageRightService.updateManageUser(Integer.parseInt(manageUserIds),
					StringUtil.getParam(isReset), 
					userRight, 
					StringUtil.getParam(isDelete));
			
			ct.put(StaticData.reKey.result.getKey(), result);
			return StaticData.RE_SUCCESS;
		} catch (Exception e) {
			LogUtil.error("获取添加比赛的页面报错",e);
			result.put(StaticData.reKey.code.getKey(), StaticData.codeType.other_error.getType());
			ct.put(StaticData.reKey.result.getKey(), result);
			return StaticData.RE_SUCCESS;
		}
	}
	
	/**
	 * 添加管理员用户
	 * @return
	 */
	public String addManageUser(){
		ActionContext ct=ActionContext.getContext();
		JSONObject result = new JSONObject();
		try {
			/*
			 * 再通过sessionid获取用户信息
			 */
			ManageUser manageUser =  ManageUtil.getManageUserBySession(ct, result, 
					request,manageService, RightUtil.getRightId(this.getClass().getName()));
			if(manageUser==null){
				
				ct.put(StaticData.reKey.result.getKey(), result);
				return StaticData.RE_SUCCESS;
			}
			String manageUserName =request.getParameter("manageUserName");
			//用户权限
			String userRight = request.getParameter("userRight");
			if(manageUserName==null){
				result.put(StaticData.reKey.code.getKey(), StaticData.codeType.param_error.getType());
				ct.put(StaticData.reKey.result.getKey(), result);
				return StaticData.RE_SUCCESS;
			}
			result = manageRightService.addManageUser(manageUserName, userRight);
			ct.put(StaticData.reKey.result.getKey(), result);
			return StaticData.RE_SUCCESS;
		} catch (Exception e) {
			LogUtil.error("获取添加比赛的页面报错",e);
			result.put(StaticData.reKey.code.getKey(), StaticData.codeType.other_error.getType());
			ct.put(StaticData.reKey.result.getKey(), result);
			return StaticData.RE_SUCCESS;
		}
	}
	
	/**
	 * 获取权限列表
	 * @return
	 */
	public String getRightList(){
		ActionContext ct=ActionContext.getContext();
		JSONObject result = new JSONObject();
		try {
			
			result = manageRightService.getRightList();
			ct.put(StaticData.reKey.result.getKey(), result);
			return StaticData.RE_SUCCESS;
		} catch (Exception e) {
			LogUtil.error("获取添加比赛的页面报错",e);
			result.put(StaticData.reKey.code.getKey(), StaticData.codeType.other_error.getType());
			ct.put(StaticData.reKey.result.getKey(), result);
			return StaticData.RE_SUCCESS;
		}
	}
}
